package com.zl.util;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

import com.zl.common.Config;

/**
 * 	@author chxl
 *	es连接工具类
 */
public class EsUtils {
	private static TransportClient client = null;
	
	public static TransportClient getClient(){
		if(null==client){
			init();
		}
		return client;
	}
	
	public static synchronized void init(){
		if(null==client){
			String clusterName = Config.getInstance().getCustomValue("cluster.name");
			String esip = Config.getInstance().getCustomValue("es.ip");
			Settings settings = ImmutableSettings.settingsBuilder()
	        .put("cluster.name", clusterName).build();
			client = new TransportClient(settings);
			client.addTransportAddress(new InetSocketTransportAddress(esip, 9300));
		}
	}
}
